﻿using System.Collections.Generic;
using System.Linq;
using Beyondsoft.Tuanju.DataManager;
using NotificationsExtensions.BadgeContent;
using NotificationsExtensions.TileContent;
using Windows.UI.Notifications;

namespace Tuan.Common
{
    public class LiveTileManager
    {
        /// <summary>
        /// Update wide Peek image tile
        /// </summary>
        /// <param name="deal">Deal deal instance</param>
        /// <param name="tileNodtificationTag">tile nodtification tag</param>
        private static void UpdateWidePeekImageTile(Deal deal, string tileNodtificationTag) 
        {
             ITileWidePeekImage01 tileContent = TileContentFactory.CreateTileWidePeekImage01();
             tileContent.Image.Src = deal.Image;
             tileContent.TextHeading.Text = deal.ShortTitle;
             tileContent.TextBodyWrap.Text = deal.Title;

             ITileSquarePeekImageAndText02 squareContent = TileContentFactory.CreateTileSquarePeekImageAndText02();
             squareContent.Image.Src = deal.Image;
             squareContent.TextHeading.Text = deal.ShortTitle;
             squareContent.TextBodyWrap.Text = deal.Title;
             tileContent.SquareContent = squareContent;

             TileNotification tileNotification = tileContent.CreateNotification();
             tileNotification.Tag = tileNodtificationTag;
             TileUpdateManager.CreateTileUpdaterForApplication().Update(tileNotification);

        }

        /// <summary>
        /// Update badge for deal count
        /// </summary>
        /// <param name="dealCount">deal count</param>
        private static void UpdateBadgeforDealCount(int dealCount)
        {
            BadgeUpdateManager.CreateBadgeUpdaterForApplication().Clear();
            BadgeNumericNotificationContent badgeContent = new BadgeNumericNotificationContent((uint)dealCount);
            BadgeUpdateManager.CreateBadgeUpdaterForApplication().Update(badgeContent.CreateNotification());
        }

        /// <summary>
        /// Initialize tile and badge
        /// </summary>
        public static async void InitializeTileAndBadge()
        {
            TileUpdateManager.CreateTileUpdaterForApplication().Clear();
            TileUpdateManager.CreateTileUpdaterForApplication().EnableNotificationQueue(true);

            IEnumerable<Deal> tileDeals = await City.DefaultCity.GetRecommends(5);
            int allDealsCount = (await City.DefaultCity.GetDeals()).Count();

            UpdateBadgeforDealCount(allDealsCount);

            foreach (Deal deal in tileDeals)
            {
                UpdateWidePeekImageTile(deal, deal.Gid);
            }
        }
    }
}
